package io.modifier.basic.compose.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance

// 主色
val PrimaryColor = Color(0xff07C160)

// 元素颜色
val BorderColorLight = Color(0f, 0f, 0f, 0.1f)
val BorderColorDark = Color.White.copy(0.1f)
val BackgroundColorLight = Color(0xffEDEDED)
val BackgroundColorDark = Color(0xff111111)
val OnBackgroundColorLight = Color.White
val OnBackgroundColorDark = Color(0xff191919)
val DangerColorLight = Color(0xffFA5151)
val DangerColorDark = Color(0xff9A0404)
val WarningColor = Color(254, 156, 1)
val PlainColor = Color(199, 198, 205)

// 文字颜色
val FontColorLight = Color(0f, 0f, 0f, 0.9f)
val FontColorDark = Color.White.copy(0.8f)
val FontSecondaryColorLight = Color(0f, 0f, 0f, 0.55f)
val FontSecondaryColorDark = Color.White.copy(0.5f)
val FontLinkColor = Color(0xff576B95)


interface ColorAsset {
    val appColor: Color
        get() = Color(0xff0079FC)
    val onAppColor: Color
        get() = Color(0xffFFFFFF)

    val appLightContainer: Color
        get() = appColor.copy(alpha = .5f)

    val bgLight: Color
        get() = Color(0xffFFFFFF)
    val bgColor: Color
        get() = Color(0xffF7F7F7)
    val bgBrush: Brush
        get() = Brush.verticalGradient(
            listOf(
                bgLight,
                bgColor,
                appColor,
            )
        )
    val onBgColor: Color
        get() = Color(0xff161A1B)

    val surface: Color
        get() = Color(0xffFFFFFF)
    val onSurface: Color
        get() = Color(0xff161A1B)

    val grayContainer: Color
        get() = onGrayContainer.copy(alpha = .15f)
    val onGrayContainer: Color
        get() = Color(0xFFA5AAB2)
    val grayLighter: Color
        get() = onGrayContainer.copy(alpha = .3f)

    val dividerColor: Color
        get() = Color(0xffF2F6FA)

    val txtColor: Color
        get() = Color(0xff161A1B)
    val txtGray: Color
        get() = Color(0xff8D8D8D)

    val error: Color
        get() = Color(0xffFF3B30)
    val onError: Color
        get() = Color(0xffFFFFFF)

    val success: Color
        get() = Color(0xff00C75A)
    val onSuccess: Color
        get() = Color(0xffFFFFFF)

}

internal object LightColorAsset : ColorAsset

internal object DarkColorAsset : ColorAsset {

    override val bgColor: Color
        get() = Color(0xff161A1B)

    override val bgLight: Color
        get() = Color(0xff24282A)

    override val dividerColor: Color
        get() = Color(0xffF3F4F8)

    override val txtColor: Color
        get() = Color(0xffffffff)
}

@Composable
fun Color.themeAdapter(): Color = if (isSystemInDarkTheme()) this.getDarkThemeColor() else this

data class HsvColor(val hue: Float, val saturation: Float, val value: Float)

fun rgbToHsv(rNormalized: Float, gNormalized: Float, bNormalized: Float): HsvColor {

    val max = maxOf(rNormalized, gNormalized, bNormalized)
    val min = minOf(rNormalized, gNormalized, bNormalized)
    val delta = max - min

    val value = max
    val saturation = if (max == 0f) 0f else delta / max

    var hue: Float = 0f
    when {
        delta == 0f -> hue = 0f
        max == rNormalized -> hue = 60f * ((gNormalized - bNormalized) / delta % 6f)
        max == gNormalized -> hue = 60f * ((bNormalized - rNormalized) / delta + 2f)
        max == bNormalized -> hue = 60f * ((rNormalized - gNormalized) / delta + 4f)
    }
    if (hue < 0) {
        hue += 360f
    }

    return HsvColor(hue, saturation, value)
}

fun Color.getDarkThemeColor(): Color {
    // 转化hsv
    val hsv = rgbToHsv(red, green, blue)
    // 转化结果
    val mHue = hsv.hue//色调范围0-360
    val mSat = hsv.saturation//饱和度范围0-1
    var mVal = hsv.value//亮度范围0-1

    // 可以自己调整大小
    mVal = 1.1f - this.luminance()

    if (mVal > 1) mVal = .93f
    if (mVal < 0) mVal = .13f
    // ColorSpace

    return Color.hsv(mHue, mSat, mVal)
}

val MaterialTheme.appColors
    @Composable
    @ReadOnlyComposable
    get() = AppThemeHolder.colors